<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>文件上传</title>
    <script src="js/jquery-3.1.1.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }

        #uploadForm {
            background-color: #ffffff;
            padding: 20px;
            margin: 50px auto;
            width: 300px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        input[type="file"],
        input[type="text"],
        input[type="button"] {
            display: block;
            margin-bottom: 15px;
            width: 100%;
            padding: 10px;
            border: 1px solid #cccccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        input[type="button"] {
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }

        input[type="button"]:hover {
            background-color: #45a049;
        }

        input[type="button"]:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        #progressContainer {
            display: none;
            margin-top: 10px;
        }

        #progressBar {
            width: 0%;
            height: 20px;
            background-color: #4CAF50;
            text-align: center;
            line-height: 20px;
            color: white;
            border-radius: 5px;
        }

        #homeButton {
            text-align: center;
            margin-top: 20px;
        }

        #homeButton a {
            display: inline-block;
            text-decoration: none;
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        #homeButton a:hover {
            background-color: #0056b3;
        }

        .file-tip {
            font-size: 12px;
            color: #999;
            margin-top: -10px;
            margin-bottom: 15px;
        }
    </style>
</head>

<body>

<form id="uploadForm" enctype="multipart/form-data">
    <label for="filename">文件上传：</label>
    <input type="file" name="filename" id="filename" />
    <!-- 添加文件格式提示 -->
    <p class="file-tip">仅支持 wav、mp3、m4a、mp4、flac 格式音频</p>
    <label for="singer">歌手名：</label>
    <input type="text" name="singer" id="singer" placeholder="请输入歌手名" />
    <input type="button" value="上传" id="uploadButton" />

    <!-- 进度条 -->
    <div id="progressContainer">
        <div id="progressBar">0%</div>
    </div>

    <div id="homeButton">
        <a href="list.html">回到首页</a>
    </div>
</form>

<script>
    $(document).ready(function () {
        $('#uploadButton').click(function (e) {
            e.preventDefault(); // 阻止按钮的默认行为
            var singer = $('#singer').val();
            // 校验文件和歌手名
            if (!$('#filename').val()) {
                alert('请选择要上传的文件！');
                return;
            }
            if (!singer) {
                alert('歌手名不能为空！');
                return;
            }

            // 获取选择的文件
            var file = $('#filename')[0].files[0];
            // 定义允许的文件格式
            var allowedFormats = ['wav', 'mp3', 'm4a', 'mp4', 'flac'];
            // 获取文件扩展名
            var fileExtension = file.name.split('.').pop().toLowerCase();

            // 校验文件格式
            if (!allowedFormats.includes(fileExtension)) {
                alert('仅支持 wav、mp3、m4a、mp4、flac 格式音频，请重新选择！');
                return;
            }

            var formData = new FormData($('#uploadForm')[0]);

            var xhr = new XMLHttpRequest();
            xhr.open("POST", "/music/upload", true);

            // 禁用上传按钮，防止重复点击
            $('#uploadButton').prop('disabled', true);
            $("#progressContainer").show();  // 显示进度条

            // 监听上传进度
            xhr.upload.onprogress = function (event) {
                if (event.lengthComputable) {
                    var percentComplete = (event.loaded / event.total) * 100;
                    $("#progressBar").css("width", percentComplete + "%").text(Math.round(percentComplete) + "%");
                }
            };

            // 监听请求完成
            xhr.onload = function () {
                if (xhr.status == 200) {
                    var response = JSON.parse(xhr.responseText);
                    if (response.status == -1) {
                        alert('暂不支持该类型文件！');
                    } else {
                        alert('上传成功！');
                    }
                } else if (xhr.status === 401) {
                    alert("您尚未登录，请先登录！");
                    window.location.href = "/login.html";
                } else {
                    alert('上传失败，请稍后重试！');
                }

                // 上传完成后恢复按钮
                $('#uploadButton').prop('disabled', false);
                $("#progressContainer").hide();
                $("#progressBar").css("width", "0%").text("0%");
            };

            // 处理错误
            xhr.onerror = function () {
                alert('上传出错，请检查网络连接！');
                $('#uploadButton').prop('disabled', false);
                $("#progressContainer").hide();
                $("#progressBar").css("width", "0%").text("0%");
            };

            xhr.send(formData);
        });
    });
</script>

</body>

</html>